package com.hp.nacos.config.web;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.hp.common.common.Result;
import com.hp.nacos.config.entity.po.User;
import com.hp.nacos.config.service.UserService;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dongxing
 * @since 2019-05-19
 */
@Api(value = "/config/user", tags = {"用户管理"})
@RestController
@RequestMapping("/config/user")
@AllArgsConstructor(onConstructor_={@Autowired})
@Slf4j
public class UserController {

    private final UserService userService;

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "id", value = "")
    })
    @ApiOperation(value = "根据用户的id查询用户的信息", notes = "", httpMethod = "GET")
    @GetMapping("/{id}")
    @ApiImplicitParam(name = "id", value = "主键ID", required = true, defaultValue = "1", dataType = "String", paramType = "path")
    @ApiResponses({
            @ApiResponse(code = 200, message = "Successful - 请求已完成"),
            @ApiResponse(code = 400, message = "请求中有语法问题，或不能满足请求"),
            @ApiResponse(code = 401, message = "未认证客户机访问数据"),
            @ApiResponse(code = 403, message = "未授权客户机访问数据"),
            @ApiResponse(code = 404, message = "服务器找不到指定的资源"),
            @ApiResponse(code = 500, message = "服务器不能完成请求")
    })
    public Result<User> findUserById(@PathVariable Integer id) {
        User byId = userService.getById(id);
        return Result.createBySuccess(byId);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "pageNum", value = ""),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "pageSize", value = "")
    })
    @ApiOperation(value = "分页查询用户的信息", notes = "", httpMethod = "POST")
    @PostMapping("/findUsers")
    @ApiResponses({
            @ApiResponse(code = 200, message = "Successful - 请求已完成"),
            @ApiResponse(code = 400, message = "请求中有语法问题，或不能满足请求"),
            @ApiResponse(code = 401, message = "未认证客户机访问数据"),
            @ApiResponse(code = 403, message = "未授权客户机访问数据"),
            @ApiResponse(code = 404, message = "服务器找不到指定的资源"),
            @ApiResponse(code = 500, message = "服务器不能完成请求")
    })
    public Result<IPage<User>> findUserByPage(Integer pageNum, Integer pageSize, HttpServletRequest request) {
        IPage<User> userIPage = this.userService.page(PageDTO.of(pageNum,pageSize), new LambdaQueryWrapper<User>().orderByDesc(User::getId));
        return Result.createBySuccess(userIPage);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "User", name = "user", value = "用户实体",dataTypeClass = User.class)
    })
    @ApiOperation(value = "", notes = "", httpMethod = "POST")
    // 如果是以@RequestBody形式接收参数，那么ignoreParameters中填写参数名.要忽略的属性名即可
    @ApiOperationSupport(ignoreParameters = {"user.id", "user.age"})
    @PostMapping("/user")
    @ApiResponses({
            @ApiResponse(code = 200, message = "Successful - 请求已完成"),
            @ApiResponse(code = 400, message = "请求中有语法问题，或不能满足请求"),
            @ApiResponse(code = 401, message = "未认证客户机访问数据"),
            @ApiResponse(code = 403, message = "未授权客户机访问数据"),
            @ApiResponse(code = 404, message = "服务器找不到指定的资源"),
            @ApiResponse(code = 500, message = "服务器不能完成请求")
    })
    public Result<User> findUserByPage(@RequestBody User user) {
        return Result.createBySuccess(user);
    }

    @Autowired
    private RetryTemplate retryTemplate;
    @GetMapping("/retry")
    public void retry(){
        //this.userService.retryTest(200, "你好");
        retryTemplate.execute(context -> {
            this.userService.retryTest(200, "你好");
            return null;
        }, context -> {
            log.error("Recovering from {}", context.getLastThrowable().getMessage());
            return null;
        });
    }



}
